package quenue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

class MySource {

    private volatile boolean FLAG = true;//默认开启生产和消费
    private AtomicInteger atomicInteger = new AtomicInteger();

    BlockingQueue<String> blockingQueue = null;

    public MySource(BlockingQueue<String> blockingQueue) {

        this.blockingQueue = blockingQueue;
        System.out.println(blockingQueue.getClass().getName());
    }

    public void myProd() throws Exception {
        String data = null;
        boolean retValue;
        while (FLAG) {
            data = atomicInteger.incrementAndGet()+"";
            retValue =blockingQueue.offer(data,2, TimeUnit.SECONDS);

            if(retValue) {
                System.out.println(Thread.currentThread().getName() + "\t 插入队列" + data + "成功");
            } else {
                System.out.println(Thread.currentThread().getName() + "\t 插入队列" + data + "失败");
            }
            TimeUnit.SECONDS.sleep(1);
        }
        System.out.println(Thread.currentThread().getName() + "\t 大老板叫停了，表示FLAG = false，生产动作结束");
    }

    public void myConsumer() throws Exception {

        String result = null;
        while (FLAG) {
            result = blockingQueue.poll(2L, TimeUnit.SECONDS);

            if(null == result || result.equalsIgnoreCase("")) {

                FLAG = false;
                System.out.println(Thread.currentThread().getName() + "\t 超过2秒钟没取到蛋糕，消费退出");
                System.out.println();
                System.out.println();
                return;

            }
            System.out.println(Thread.currentThread().getName() + "\t 消费队列蛋糕" + result + "成功");

        }

    }

    public void stop() throws Exception {

        this.FLAG = false;
    }

}


public class ProdConsumer_New {

    public static void main(String[] args) throws Exception {

        MySource mySource = new MySource(new ArrayBlockingQueue<>(10));

            new Thread(() -> {
                    //线程要执行的代码
                System.out.println(Thread.currentThread().getName() + "\t 生产线程启动");
                try {
                    mySource.myProd();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            },"Product").start();

        new Thread(() -> {
            //线程要执行的代码
            System.out.println(Thread.currentThread().getName() + "\t 生产线程启动");
            System.out.println();
            System.out.println();
            try {
                mySource.myConsumer();
                System.out.println();
                System.out.println();
            } catch (Exception e) {
                e.printStackTrace();
            }

        },"Consumer").start();
        try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); }
        System.out.println();
        System.out.println();
        System.out.println();

        System.out.println("5秒钟时间到，main线程叫停，活动结束");

        mySource.stop();



    }
}
